﻿/*-----------------------------------------------
// File: BCStateCache.cs
// Description: 大招时，对象前一状态的缓存
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Battle.View
{
    public interface BCStateCache
    {
        ActorState GetCacheState();
        void Revert();
    }
    public class BCStateCacheFactory {
        public static BCStateCache GetBCStateCache(BattleCharacter bc) {
            /*
            switch (bc.GetCurrrentStateType()) {
                case ActorState.Attack:
                case ActorState.AttackMoveForward:
                case ActorState.AttackWait:
                    return new BCAttackWaitStateCache(bc);
                case ActorState.AttackReady:
                case ActorState.AttackMoveBackward:
                    return new BCAttackReadyStateCache(bc);
                case ActorState.Dead:
                    return new BCDeadStateCache(bc);
                case ActorState.Defend:
                    return new BCDefendStateCache(bc);
                default:
                    Debug.LogError("[BCStateCacheFactory] Create Cache BC in error State: " + bc.GetCurrrentStateType().ToString());
                    return null;
            }
            */
            switch (bc.GetCurrrentStateType()) {
                case ActorState.Attack:
                case ActorState.AttackMoveForward:
                case ActorState.AttackWait:
                case ActorState.AttackReady:
                case ActorState.AttackMoveBackward:
                    return new BCAttackReadyStateCache(bc);
                case ActorState.Dead:
                    return new BCDeadStateCache(bc);
                case ActorState.Defend:
                    return new BCDefendStateCache(bc);
                default:
                    Debug.LogError("[BCStateCacheFactory] Create Cache BC in error State: " + bc.GetCurrrentStateType().ToString());
                    return null;
            }
        }
        public static List<AttackRespect> GetBCLastRepsects(BattleCharacter bc) {
            List<AttackRespect> res = new List<AttackRespect>();
            if (bc.GetCurrrentStateType() == ActorState.AttackMoveForward)
            {
                var package = bc.GetCurrentPackage();
                var skill = bc.GetSkill(package.skillType);
                if (skill != null)
                {
                    foreach (var release in skill.releases)
                    {
                        res.Add(new AttackRespect(bc.uniqueId, skill.skillType, release.HSRId));
                    }
                }
            }
            else if (bc.GetCurrrentStateType() == ActorState.Attack) {
                var state = bc.GetCurrentState() as BCAttackState;
                foreach (var release in state.GetLastReleases()) {
                    res.Add(new AttackRespect(release.HeroUid, release.SkillType, release.HSRId));
                }
            }
            return res;
        }

    }

    /// <summary>
    /// AttackReady
    /// </summary>
    public class BCAttackReadyStateCache : BCStateCache {
        public ActorState GetCacheState() { return ActorState.AttackReady; }

        public string actorUId;
        public BCAttackReadyStateCache(BattleCharacter bc) {
            actorUId = bc.uniqueId;
        }
        public void Revert() {
            var bc = BattleViewCtrl.GetInstance().GetBattleCharacter(actorUId);
            bc.gameObject.SetActive(true);
            bc.transform.SetParent(BattleViewCtrl.GetInstance().transform);
            bc.PlayStand(true);
            bc.SwitchState(ActorState.AttackReady);
        }
    }

    /// <summary>
    /// AttackWait Attack AttackMoveForward
    /// </summary>
    public class BCAttackWaitStateCache : BCStateCache {
        public ActorState GetCacheState() { return ActorState.AttackWait; }
        public Vector3 waitPosition;
        public string actorUid;
        public BCAttackWaitStateCache(BattleCharacter bc) {
            actorUid = bc.uniqueId;
            if (bc.GetCurrrentStateType() == ActorState.Attack ||
                bc.GetCurrrentStateType() == ActorState.AttackWait)
            {
                waitPosition = bc.transform.localPosition;
            }
            else if (bc.GetCurrrentStateType() == ActorState.AttackMoveForward)
            {
                var state = bc.GetCurrentState() as BCAttackMoveForwardState;
                waitPosition = state.GetTargetPosition();
            }
            else {
                Debug.LogError("[BCAttackWaitStateCache] BattleCharacter Use BCAttackWaitStateCache in wrong State:" + bc.GetCurrrentStateType().ToString());
                waitPosition = Vector3.zero;
            }
        }
        public void Revert() {
            var bc = BattleViewCtrl.GetInstance().GetBattleCharacter(actorUid);
            bc.gameObject.SetActive(true);
            bc.transform.SetParent(BattleViewCtrl.GetInstance().transform);
            bc.transform.localPosition = waitPosition;
            bc.transform.localScale = Vector3.one;
            bc.transform.localEulerAngles = bc.GetStandEulerAngle();
            bc.PlayStand(true);
            bc.SwitchState(ActorState.AttackWait);
        }
    }

    /// <summary>
    /// Dead
    /// </summary>
    public class BCDeadStateCache : BCStateCache {
        public ActorState GetCacheState() { return ActorState.Dead; }
        public string actorUid;
        public BCDeadStateCache(BattleCharacter bc) {
            actorUid = bc.uniqueId;
        }
        public void Revert() {
            var bc = BattleViewCtrl.GetInstance().GetBattleCharacter(actorUid);
            bc.gameObject.SetActive(true);
            bc.transform.SetParent(BattleViewCtrl.GetInstance().transform);
            bc.transform.localPosition = bc.GetStandPosition();
            bc.transform.localEulerAngles = bc.GetStandEulerAngle();
            bc.transform.localScale = Vector3.one;
            bc.SwitchState(ActorState.Dead);
        }
    }

    /// <summary>
    /// Defend
    /// </summary>
    public class BCDefendStateCache : BCStateCache {
        public ActorState GetCacheState() { return ActorState.Defend; }
        public string actorUid;
        public BCDefendStateCache(BattleCharacter bc) {
            actorUid = bc.uniqueId;
        }
        public void Revert() {
            var bc = BattleViewCtrl.GetInstance().GetBattleCharacter(actorUid);
            bc.gameObject.SetActive(true);
            bc.transform.SetParent(BattleViewCtrl.GetInstance().transform);
            bc.transform.localPosition = bc.GetStandPosition();
            bc.transform.localEulerAngles = bc.GetStandEulerAngle();
            bc.transform.localScale = Vector3.one;
            bc.PlayStand(true);
            bc.SwitchState(ActorState.Defend);
        }
    }

    

}